<?php 
namespace app\admin\controller;

use think\Exception;
use think\Db;
use app\common\model\shop\ShopCoupon;
use app\common\controller\Log;

class Coupon extends Base
{
	/**
	 * 添加优惠券
	 * @throws Exception
	 * @return unknown
	 */
	public function addCoupon()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			if(!$data['coupon_name'])throw new Exception('请输入优惠券名称');
			if($data['coupon_scope'] == 1 && !$data['goods_id'])throw new Exception('请选择商品');
			if($data['coupon_scope'] == 2 && !$data['classify_id'])throw new Exception('请选择分类');
			if(!$data['coupon_content'])throw new Exception('请输入优惠券优惠');
			if(!$data['coupon_num'])throw new Exception('请输入优惠券发布数量');
			if(!$data['start_time'] || !$data['end_time'])throw new Exception('请选择可用时间');
			$data['surplus_num'] = $data['coupon_num'];
			$data['create_time'] = time();
			//添加优惠券
			ShopCoupon::create($data);
			//添加日志
			Log::addLog(1,'添加优惠券【'.$data['coupon_name'].'】');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('添加成功');
	}
	
	/**
	 * 优惠券列表
	 * @return unknown
	 */
	public function couponList()
	{
		$list = ShopCoupon::order('end_time desc,id desc')->paginate()->toArray();
		foreach ($list['data'] as &$rows) {
			if($rows['start_time'] > time())$state = 0;
			elseif($rows['start_time'] < time() && $rows['end_time'] > time())$state = 1;
			else $state = 2;
			$rows['state'] = $state;
			$rows['start_time'] = date('Y-m-d',$rows['start_time']);
			$rows['end_time'] = date('Y-m-d',$rows['end_time']);
		}
		return jsonSuccess($list);
	}
	
	/**
	 * 设置优惠券置顶状态
	 * @return unknown
	 */
	public function setIsTop()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			$coupon = ShopCoupon::get($id);
			$update = [];
			$update['is_top'] = $coupon->is_top ? 0 : 1;
			$msg = $coupon->is_top ? '取消置顶' : '设置置顶';
			if($update['is_top'] == 1){
				$count = ShopCoupon::where(['start_time' => ['lt',time()],'end_time' => ['gt',time()],'is_top' => 1])->count();
				if($count >= 3)throw new Exception('最多三个置顶'); 
			}
			$coupon->save($update);
			//添加日志
			Log::addLog(2, $msg."优惠券【{$coupon->coupon_name}】");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess($msg.'成功');
	}
	
	/**
	 * 修改优惠券剩余数量
	 * @return unknown
	 */
	public function updateSurplusNum()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			$coupon = ShopCoupon::get($data['id']);
			//修改剩余数量时更新发行优惠券的总数量
			$len = $coupon->coupon_num - $coupon -> surplus_num;
			$data['coupon_num'] = $data['surplus_num'] + $len;
			$coupon->save($data);
			//添加日志
			Log::addLog(2, "修改优惠券【{$coupon->coupon_name}】的剩余数量为【".$data['surplus_num']."】");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('剩余数量修改成功');
	}
}